package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.mapper.DeviceSupplierMapper;
import com.eastjet.equipment.service.DeviceSupplierService;
import com.eastjet.maintenance.domain.DeviceMel;
import com.eastjet.maintenance.domain.DeviceSupplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 设备供应商基础信息*/
@CrossOrigin
@RestController
@RequestMapping("/supplier")
public class BasicInformationController {

    private static final Logger log = LoggerFactory.getLogger(BasicInformationController.class);
    @Autowired
    private DeviceSupplierService deviceSupplierService;

    /**
     * 获取供应商列表(所有)
     */
    @RequestMapping("/list")
    public Page<DeviceSupplier> getSupplierList(@RequestParam(defaultValue = "1") int page,
                                                @RequestParam(defaultValue = "4") int size,
                                                @RequestParam(defaultValue ="") String condition
    ) {
        System.out.println("condition:" + condition);
        return  deviceSupplierService.getSupplierList(page, size,condition);
    }

    @RequestMapping("/add")
    public String add(@RequestBody Map<String, Object> map) {
        System.out.println("map:" + map);
        String TableName;
        System.out.println("TableName:"+(String) map.get("TableName"));
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        if (map.get("TableName") == null || map.isEmpty()){
            //返回错误信息
            return "TableName is null or empty";
        }else {
            TableName = (String) map.get("TableName");
            DeviceSupplier deviceSupplier = new DeviceSupplier();
            deviceSupplier.setCode((String) map.get("code"));
            // 使用 Integer.valueOf 转换整数
            deviceSupplier.setSupplierName((String) map.get("supplierName"));
            if (map.get("supplierType").equals("备件")){
                deviceSupplier.setSupplierType("1");
            }else {
                deviceSupplier.setSupplierType("0");
            }
            deviceSupplier.setSupplierPerson((String) map.get("supplierPerson"));
            deviceSupplier.setSupplierPhone((String) map.get("supplierPhone"));
            deviceSupplier.setBank((String) map.get("bank"));
            deviceSupplier.setBankAccount((String) map.get("bankAccount"));
            deviceSupplier.setSupplierWx((String) map.get("supplierWx"));
            if ((map.get("supplierState").toString()).equals("停用")){
                deviceSupplier.setSupplierState(1);
            }else {
                deviceSupplier.setSupplierState(0);
            }

            deviceSupplier.setSupplierNoet((String) map.get("supplierNoet"));
            deviceSupplier.setAddress((String) map.get("address"));
            System.out.println(12345789);
            return deviceSupplierService.addDeviceSupplier(deviceSupplier,TableName);
        }
    }

    @RequestMapping("/update")
    public String update(@RequestBody Map<String, Object> map)
    {

        System.out.println("map:" + map);

            DeviceSupplier deviceSupplier = new DeviceSupplier();
            deviceSupplier.setCode((String) map.get("code"));
            // 使用 Integer.valueOf 转换整数
            deviceSupplier.setSupplierName((String) map.get("supplierName"));
            deviceSupplier.setSupplierId(Integer.valueOf(map.get("supplierId").toString()));
            deviceSupplier.setSupplierType((String) map.get("supplierType"));
            deviceSupplier.setSupplierPerson((String) map.get("supplierPerson"));
            deviceSupplier.setSupplierPhone((String) map.get("supplierPhone"));
            deviceSupplier.setSupplierWx((String) map.get("supplierWx"));
            deviceSupplier.setBank((String) map.get("bank"));
            deviceSupplier.setBankAccount((String) map.get("bankAccount"));
            if ((map.get("supplierState").toString()).equals("停用")) {
                deviceSupplier.setSupplierState(1);
            } else {
                deviceSupplier.setSupplierState(0);
            }
            deviceSupplier.setSupplierNoet((String) map.get("supplierNoet"));
            deviceSupplier.setAddress((String) map.get("address"));
            System.out.println("1234567890");
            return deviceSupplierService.updateDeviceSupplier(deviceSupplier);
    }

    @RequestMapping("/delete")
    public String delete(@RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceSupplier deviceSupplier = new DeviceSupplier();
        deviceSupplier.setSupplierId(Integer.valueOf(map.get("supplierId").toString()));
        deviceSupplier.setIsDelete(Integer.valueOf(map.get("isDelete").toString()));
        return deviceSupplierService.deleteDeviceSupplier(deviceSupplier);
    }

    @RequestMapping("/updateState")
    public String updateState(@RequestBody Map<String, Object> map) {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceSupplier deviceSupplier = new DeviceSupplier();
        deviceSupplier.setSupplierId(Integer.valueOf(map.get("supplierId").toString()));
        deviceSupplier.setSupplierState(Integer.valueOf(map.get("supplierState").toString()));
        return deviceSupplierService.updateDeviceSupplierState(deviceSupplier);
    }
}
